package org.alwayssuper.protocol.commons.transform.realtimev2016.enums;

/**
 * 通用报警标志位枚举 (报警数据 0x07)
 * 基于 GB/T 32960.3-2016 标准
 * 32位报警标志位定义 (DWORD类型)
 * @author alwaysSuper
 */
public enum CommonAlarmFlags {
    
    // Bit 0-15
    TEMPERATURE_DIFFERENCE_ALARM(0, "温度差异报警"),
    BATTERY_HIGH_TEMP_ALARM(1, "电池高温报警"), 
    VEHICLE_STORAGE_DEVICE_OVERVOLTAGE_ALARM(2, "车载储能装置类型过压报警"),
    VEHICLE_STORAGE_DEVICE_UNDERVOLTAGE_ALARM(3, "车载储能装置类型欠压报警"),
    SOC_LOW_ALARM(4, "SOC低报警"),
    SINGLE_BATTERY_OVERVOLTAGE_ALARM(5, "单体电池过压报警"),
    SINGLE_BATTERY_UNDERVOLTAGE_ALARM(6, "单体电池欠压报警"),
    SOC_HIGH_ALARM(7, "SOC过高报警"),
    SOC_JUMP_ALARM(8, "SOC跳变报警"),
    RECHARGEABLE_STORAGE_SYSTEM_MISMATCH_ALARM(9, "可充电储能系统不匹配报警"),
    BATTERY_CELL_CONSISTENCY_ALARM(10, "电池单体一致性差报警"),
    INSULATION_ALARM(11, "绝缘报警"),
    DC_DC_TEMPERATURE_ALARM(12, "DC-DC温度报警"),
    BRAKE_SYSTEM_ALARM(13, "制动系统报警"),
    DC_DC_STATUS_ALARM(14, "DC-DC状态报警"),
    DRIVE_MOTOR_CONTROLLER_TEMP_ALARM(15, "驱动电机控制器温度报警"),
    
    // Bit 16-18
    HIGH_VOLTAGE_INTERLOCK_ALARM(16, "高压互锁状态报警"),
    DRIVE_MOTOR_TEMP_ALARM(17, "驱动电机温度报警"),
    VEHICLE_STORAGE_DEVICE_OVERCHARGE_ALARM(18, "车载储能装置类型过充");
    
    // Bit 19-31 预留

    private final int bitPosition;
    private final String description;

    CommonAlarmFlags(int bitPosition, String description) {
        this.bitPosition = bitPosition;
        this.description = description;
    }

    public int getBitPosition() {
        return bitPosition;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 获取该标志位的掩码值
     * @return 掩码值
     */
    public int getMask() {
        return 1 << bitPosition;
    }

    /**
     * 检查指定标志位是否设置
     * @param alarmFlags 4字节报警标志
     * @param flag 要检查的标志
     * @return true=标志位已设置
     */
    public static boolean isAlarmSet(int alarmFlags, CommonAlarmFlags flag) {
        return (alarmFlags & flag.getMask()) != 0;
    }

    /**
     * 设置指定标志位
     * @param alarmFlags 当前报警标志
     * @param flag 要设置的标志
     * @return 设置后的报警标志
     */
    public static int setAlarmFlag(int alarmFlags, CommonAlarmFlags flag) {
        return alarmFlags | flag.getMask();
    }

    /**
     * 清除指定标志位
     * @param alarmFlags 当前报警标志
     * @param flag 要清除的标志
     * @return 清除后的报警标志
     */
    public static int clearAlarmFlag(int alarmFlags, CommonAlarmFlags flag) {
        return alarmFlags & (~flag.getMask());
    }

    /**
     * 解析所有设置的报警标志
     * @param alarmFlags 4字节报警标志
     * @return 所有设置的报警标志数组
     */
    public static CommonAlarmFlags[] parseActiveAlarms(int alarmFlags) {
        return java.util.Arrays.stream(values())
                .filter(flag -> isAlarmSet(alarmFlags, flag))
                .toArray(CommonAlarmFlags[]::new);
    }

    /**
     * 格式化报警标志为可读字符串
     * @param alarmFlags 4字节报警标志
     * @return 格式化字符串
     */
    public static String formatAlarmFlags(int alarmFlags) {
        CommonAlarmFlags[] activeAlarms = parseActiveAlarms(alarmFlags);
        if (activeAlarms.length == 0) {
            return "无报警";
        }
        
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < activeAlarms.length; i++) {
            if (i > 0) sb.append(", ");
            sb.append(activeAlarms[i].getDescription());
        }
        return sb.toString();
    }

    @Override
    public String toString() {
        return String.format("CommonAlarmFlags{bit=%d, description='%s'}", bitPosition, description);
    }
}